import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:fate_blind_box/utils/confirm_alert.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';

import 'black_user_list_controller.dart';
import 'black_user_model.dart';

class BlackUserListPage extends StatefulWidget {
  const BlackUserListPage({Key? key}) : super(key: key);

  @override
  State<BlackUserListPage> createState() => _BlackUserListPageState();
}

class _BlackUserListPageState extends State<BlackUserListPage> {
  BlackUserListController listController = Get.put(BlackUserListController());

  @override
  Widget build(BuildContext context) {
    return AppWidget.navTitleBar(
      title: '黑名单',
      childWidget: GetBuilder(
          id: listController.itemViewId,
          builder: (BlackUserListController controller) {
            return controller.userList.isEmpty
                ? Container()
                : ListView.builder(
                    itemCount: listController.userList.length,
                    itemBuilder: (BuildContext context, int index) {
                      BlackUserModelData itemModel =
                          listController.userList[index];
                      return Column(
                        children: [
                          Container(
                            color: ThemeColor.lineColor,
                            height: 0.5,
                          ),
                          const SizedBox(
                            height: 15,
                          ),
                          GestureDetector(
                            onTap: () {
                              ConfirmAlertDialog.showView(context,
                                  content: '解除以后会收到对方消息，确定要解除吗',
                                  rightAction: () async{
                                try {
                                  await EMClient.getInstance.contactManager
                                      .removeUserFromBlockList(itemModel.easeuserId??'');
                                  listController.userList.removeAt(index);
                                  listController
                                      .update([listController.itemViewId]);
                                } on EMError catch (e) {
                                  debugPrint("error code: ${e.code}, desc: ${e.description}");
                                }

                              });
                            },
                            child: Container(
                              color: Colors.white,
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    itemModel.nickName ?? '',
                                    style: TextStyle(fontSize: 15),
                                  ),
                                  Text(
                                    '解除',
                                    style: TextStyle(
                                        fontSize: 15,
                                        color: ThemeColor.appColor),
                                  )
                                ],
                              ),
                            ),
                          ),
                          const SizedBox(
                            height: 15,
                          ),
                        ],
                      );
                    });
          }),
    );
  }

  @override
  void initState() {
    super.initState();
    listController.initData();
  }
}


